package com.bogewang;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
//@EnableApolloConfig
public class AppGateWay {
    @ApolloConfig
    private Config appconfig;

    public static void main(String[] args) {
        SpringApplication.run(AppGateWay.class, args);
    }
    
    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider{

        @Override
        public List<SwaggerResource> get() {
            //开启监听, 配置文件发生改变需要改变;
            //appconfig.addChangeListener((changeEvent)->get());
            ArrayList<SwaggerResource> resources = new ArrayList<>();
            resources.add(swaggerResource("app-bogewang-member", "/app-bogewang-member/v2/api-docs", "2.0"));
            resources.add(swaggerResource("app-bogewang-weixin", "/app-bogewang-weixin/v2/api-docs", "2.0"));

            //return resources();
            return resources;
        }

        private SwaggerResource swaggerResource(String name, String location, String version) {
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setLocation(location);
            swaggerResource.setSwaggerVersion(version);
            return swaggerResource;
        }

        /**
         * 从配置中心获取resources;
         * @return
         */
        private List<SwaggerResource> resources(){
            ArrayList<SwaggerResource> resources = new ArrayList<>();
            String swaggerDocJson = swaggerDocument();
            JSONArray jsonArray = JSONArray.parseArray(swaggerDocJson);

            for (Object obj : jsonArray){
                JSONObject jsonObject = (JSONObject)obj;
                String name = jsonObject.getString("name");
                String location = jsonObject.getString("location");
                String version = jsonObject.getString("version");
                resources.add(swaggerResource(name, location, version));
            }
            return resources;
        }

        private String swaggerDocument(){
            String property = appconfig.getProperty("bogewang.zuul.swaggerDocument", "");
            return property;
        }
    }

}
